package com.quarkboom.kotlinapp.intercept

/**
 * @author: zhuozhang6
 * @date: 2022/5/12
 * @email: zhuozhang6@iflytek.com
 *
 * 手写okhttp职责链模式
 */
class InterceptTest {

    companion object {
        @JvmStatic
        fun main(args: Array<String>) {

            //okhttp网上高仿例子：https://www.jianshu.com/p/1ead82e08396
            val leaderManager = RealInterceptorChain()//传递下发任务可执行等级
            leaderManager.addLeaders(InterceptorOne())
            leaderManager.addLeaders(InterceptorTwo())
            val result = leaderManager.proceed("开始请求了")//开始执行
            println("最后的结果是：$result")




            //自己模仿手写例子，response还未包装
            val manager = Manager()
            manager.addIntercept(object : InterceptInterface{
                override fun addString(manager: Manager): String {
                    manager.addString = manager.addString + "自定义拦截器"
                    val process = manager.process(manager.addString)
                    return process
                }
            })
            manager.addIntercept(InterceptA())
            manager.addIntercept(InterceptB())
            var process = manager.process("a")
            println(process)
        }
    }

}